package com.quan.common.bean;

import java.util.Map;

/**
 * 自定义响应数据结构 
 * 200：成功
 * 601：自定义警告（warning）
 * 602：自定义失败（failure）
 * errorMsg：500错误信息
 * errorException：500程序异常
 * 
 * @author yhaoquan
 * 
 */
public class R {

	/**
	 * 响应状态
	 */
	private Boolean flag;

	/**
	 * 响应业务状态码
	 */
	private Integer status;

	/**
	 * 响应业务状态文本
	 */
	private String statusText;

	/**
	 * 响应消息
	 */
	private String msg;

	/**
	 * 响应数据
	 */
	private Object data;

	/**
	 * 其他属性数据
	 */
	private Map<String, Object> attrs;

	/**
	 * 当前时间
	 */
	private Long timestamp = System.currentTimeMillis();
	
	public static R build(Boolean flag, Integer status, String statusText, String msg, Object data, Map<String, Object> attrs) {
		return new R(flag, status, statusText, msg, data, attrs) ;
	}

	public static R build(Boolean flag, Integer status, String statusText, String msg, Object data) {
		return new R(flag, status, statusText, msg, data, null) ;
	}
	
	public static R ok(String msg, Object data) {
		return new R(true, 200, "ok", msg, data, null);
	}
	
	public static R ok(Object data) {
		return new R(true, 200, "ok", null, data, null);
	}

	public static R ok(String msg) {
		return new R(true, 200, "ok", msg, null, null);
	}

	public static R ok() {
		return new R(true, 200, "ok", null, null, null);
	}
	
	public static R warning() {
		return new R(false, 601, "warning", null, null);
	}
	
	public static R warning(String msg) {
		return new R(false, 601, "warning", msg, null, null);
	}
	
	public static R warning(String msg, Object data) {
		return new R(false, 601, "warning", msg, data, null);
	}

	public static R failure() {
		return new R(false, 602, "failure", null, null);
	}

	public static R failure(String msg) {
		return new R(false, 602, "failure", msg, null, null);
	}

	public static R failure(String msg, Object data) {
		return new R(false, 602, "failure", msg, data, null);
	}
	
	public static R loginTimeOut() {
		return new R(false, 412, "timeout", "登录超时或未登录", null, null);
	}

	public static R loginTimeOut(String msg) {
		return new R(false, 412, "timeout", msg, null, null);
	}

	public static R errorMsg() {
		return new R(false, 500, "error", null, null, null);
	}

	public static R errorMsg(String msg) {
		return new R(false, 500, "error", msg, null, null);
	}

	public static R errorMsg(Object data) {
		return new R(false, 500, "error", null, data, null);
	}

	public static R errorMsg(String msg, Object data) {
		return new R(false, 500, "exception", msg, data, null);
	}

	public static R errorException(String msg) {
		return new R(false, 500, "exception", msg, null, null);
	}

	public static R errorException(Integer status, String msg) {
		return new R(false, status, "exception", msg, null, null);
	}

	public R() {
	}

	public R(Boolean flag, Integer status, String statusText, String msg) {
		super();
		this.flag = flag;
		this.status = status;
		this.statusText = statusText;
		this.msg = msg;
	}

	public R(Boolean flag, Integer status, String statusText, String msg, Object data) {
		super();
		this.flag = flag;
		this.status = status;
		this.statusText = statusText;
		this.msg = msg;
		this.data = data;
	}

	public R(Boolean flag, Integer status, String statusText, String msg, Object data, Map<String, Object> attrs) {
		super();
		this.flag = flag;
		this.status = status;
		this.statusText = statusText;
		this.msg = msg;
		this.data = data;
		this.attrs = attrs;
	}

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Map<String, Object> getAttrs() {
		return attrs;
	}

	public void setAttrs(Map<String, Object> attrs) {
		this.attrs = attrs;
	}

	public Long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Long timestamp) {
		this.timestamp = timestamp;
	}

	public String getStatusText() {
		return statusText;
	}

	public void setStatusText(String statusText) {
		this.statusText = statusText;
	}

	@Override
	public String toString() {
		return "JSONResult [flag=" + flag + ", status=" + status + ", statusText=" + statusText + ", msg=" + msg + ", data=" + data + ", attrs=" + attrs + ", timestamp=" + timestamp + "]";
	}

	
	
}
